package com.bskj.sj.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.bskj.common.bean.model.FindSumChargeVo;
import com.bskj.common.bean.model.ParkInfo;
import com.bskj.common.bean.model.ParkingLotCustomer;
import com.bskj.common.constants.CommonConstatns;
import com.bskj.common.utils.EncryptUtil;
import com.bskj.common.utils.LogUtils;
import com.bskj.common.utils.ResultMapUtil;
import com.bskj.common.web.BaseController;
import com.bskj.sj.bean.vo.resp.ParkDailyTransferDetailRespVo;
import com.bskj.sj.bean.vo.resp.ParkInfoRespVo;
import com.bskj.sj.bean.vo.resp.ParkTransTotalRespVo;
import com.bskj.sj.service.IParkInfoService;
import com.bskj.sj.service.IParkingLotCustomerService;
import com.bskj.sj.service.IShopCouponService;

@Controller
@RequestMapping("/parkingLotCustomer")
public class ParkingLotCustomerController extends BaseController {
    @Autowired
    private IParkingLotCustomerService parkingLotCustomerService;

    @Autowired
    private IParkInfoService parkInfoService;

    @Autowired
    private IShopCouponService shopCouponService;
    
    /**
     * 商家的收入流水页面
     *
     * @param custId
     * @return
     * @throws Exception
     */
    @RequestMapping("transTotal")
    public ModelAndView transTotal(FindSumChargeVo findSumChargeVo) throws Exception {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/revenue/transTotal");
        mv.addObject("custId", findSumChargeVo.getCustId());
        
        //页面跳转：查看详细收入流水
        JSONObject transTotalParams = new JSONObject();
        transTotalParams.put("uri", "/parkingLotCustomer/getTransTotal");
        transTotalParams.put("custId", findSumChargeVo.getCustId());
        mv.addObject("transTotalParams", EncryptUtil.encrypt(transTotalParams.toJSONString()));

        //页面跳转：查看详细收入流水
        JSONObject transDetailParams = new JSONObject();
        transDetailParams.put("uri", "/parkingLotCustomer/transDetail");
        transDetailParams.put("custId", findSumChargeVo.getCustId());
        mv.addObject("transDetailParams", EncryptUtil.encrypt(transDetailParams.toJSONString()));

        ParkingLotCustomer parkingLotCustomer = parkingLotCustomerService.get(findSumChargeVo.getCustId());
        mv.addObject("parkingLotCustomer", parkingLotCustomer);
        ParkTransTotalRespVo parkTransTotalRespVo = parkingLotCustomerService.getTransTotal(findSumChargeVo);
        LogUtils.debug(JSONObject.toJSONString(parkTransTotalRespVo));
        mv.addObject("parkTransTotalRespVo", parkTransTotalRespVo);
        return mv;
    }

    /**
     * 异步获取交易总额
     *
     * @param custId
     * @param date
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("getTransTotal")
    public Map<String, Object> getTransTotal(FindSumChargeVo findSumChargeVo) throws Exception {
        try {
            ParkTransTotalRespVo parkTransTotalRespVo = parkingLotCustomerService.getTransTotal(findSumChargeVo);
            if (parkTransTotalRespVo != null) {
                return ResultMapUtil.genSuccessDataMap(parkTransTotalRespVo);
            } else {
                return ResultMapUtil.genErrorMap("");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMapUtil.genErrorMap("获取数据失败");
        }
    }

    	
    /**
     * 商家的收入流水页面
     *
     * @param custId
     * @return
     * @throws Exception
     */
    @RequestMapping("transDetail")
    public ModelAndView transDetail(Integer custId) throws Exception {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/revenue/transDetail");
        mv.addObject("custId", custId);

        JSONObject transDetailParams = new JSONObject();
        transDetailParams.put("uri", "/parkingLotCustomer/transDetail");
        transDetailParams.put("custId", custId);
        mv.addObject("transDetailParams", EncryptUtil.encrypt(transDetailParams.toJSONString()));

        JSONObject transDetailDataParam = new JSONObject();
        transDetailDataParam.put("uri", "/parkingLotCustomer/getTransDetailData");
        transDetailDataParam.put("custId", custId);
        mv.addObject("transDetailDataParam", EncryptUtil.encrypt(transDetailDataParam.toJSONString()));

        return mv;
    }

    /**
     * 获取交易明细信息
     *
     * @param custId
     * @param page
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("getTransDetailData")
    public Map<String, Object> getTransDetailData(Integer custId, int chargeType) throws Exception {
    	List<ParkInfoRespVo> parkInfoRespVos=new ArrayList<>();
        List<ParkInfo> parkInfos = parkingLotCustomerService.getTransDetail(custId, chargeType);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (ParkInfo parkInfo : parkInfos) {
			ParkInfoRespVo parkInfoRespVo=new ParkInfoRespVo();
			parkInfoRespVo.setCarNo(parkInfo.getCarNo());
			parkInfoRespVo.setInTime(sdf.format( parkInfo.getInTime()));
			parkInfoRespVo.setOutTime(sdf.format(parkInfo.getOutTime()));
			parkInfoRespVo.setCharge(parkInfo.getCharge());
			parkInfoRespVo.setNeedPayCharge(parkInfo.getNeedPayCharge());
			if(parkInfo.getUserCouponId()!=0){
				parkInfoRespVo.setCouponMoney(shopCouponService.getCouponMoney(parkInfo.getUserCouponId()));
			}else {
				parkInfoRespVo.setCouponMoney(0);
			}
			parkInfoRespVos.add(parkInfoRespVo);
		}
        Map<String, Object> resultMap=new HashMap<>();
        resultMap.put("result", parkInfoRespVos);
        return resultMap;
    }

    /**
     * 惠付单的收入流水页面
     *
     * @param custId
     * @return
     * @throws Exception
     */
    @RequestMapping("transferDetail")
    public ModelAndView transferDetail(Integer custId, Integer page) throws Exception {
        if (page == null || page == 0) {
            page = 1;
        }

        ModelAndView mv = new ModelAndView();
        mv.setViewName("/revenue/transferDetail");
        mv.addObject("custId", custId);

        //页面跳转：查看详细收入流水
        JSONObject transDetailParams = new JSONObject();
        transDetailParams.put("uri", "/parkingLotCustomer/transDetail");
        transDetailParams.put("custId", custId);
        transDetailParams.put("checkPlatform", custId);
        mv.addObject("transDetailParams", EncryptUtil.encrypt(transDetailParams.toJSONString()));

        //收费方式（0-未支付 1-现金 2-平台代扣）
        List<ParkDailyTransferDetailRespVo> voList = parkInfoService.getDailyTransfer(custId, 2, page, CommonConstatns.DEFAULT_PAGE_SZIE);
        mv.addObject("transDetails", voList);
        return mv;
    }


}
